package me.firstapp.service.system.impl;

import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.module.system.Statistics;
import me.firstapp.repository.bbs.MemberRepository;
import me.firstapp.repository.bbs.TopicRepository;
import me.firstapp.repository.shop.OrderRepository;
import me.firstapp.repository.shop.PointRechargeCardRepository;
import me.firstapp.repository.system.StatisticsRepository;
import me.firstapp.service.system.StatisticsService;

@Service
public class StatisticsServiceImpl implements StatisticsService {

	@Autowired
	private StatisticsRepository statisticsRepository;

	@Autowired
	private PointRechargeCardRepository pointRechargeCardRepository;

	@Autowired
	private OrderRepository orderRepository;

	@Autowired
	private TopicRepository topicRepository;

	@Autowired
	private MemberRepository memberRepository;

	@Override
	public Statistics findStatisticsByClientId(Long apiClientId) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		return statisticsRepository.findStatisticsByClientId(apiClientId);
	}

	@Override
	public Statistics executeStatisticsByClientId(Long apiClientId, boolean isManager) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		Statistics statistics = statisticsRepository.findStatisticsByClientId(apiClientId);
		Date now = Calendar.getInstance().getTime();
		Date yesterday = DateUtils.getBeforeOrNextDay(now, -1);
		Date weekStartDay = DateUtils.getWeekStartDay(now);
		Date weekEndDay = DateUtils.getWeekEndDay(now);
		Date monthStartDay = DateUtils.getMonthStartDay(now);
		Date monthEndDay = DateUtils.getMonthEndDay(now);

		boolean flag = true;
		if (statistics == null) {
			flag = false;
			statistics = new Statistics();
		}
		if (isManager) {
			Long todayCardRechargedCounts = pointRechargeCardRepository.findCardCountsByRechargeTime(now, now);
			Long yesterdayCardRechargedCounts = pointRechargeCardRepository.findCardCountsByRechargeTime(yesterday,
					yesterday);
			Long weekCardRechargedCounts = pointRechargeCardRepository.findCardCountsByRechargeTime(weekStartDay,
					weekEndDay);
			Long monthCardRechargedCounts = pointRechargeCardRepository.findCardCountsByRechargeTime(monthStartDay,
					monthEndDay);
			Long totalCardRechargedCounts = pointRechargeCardRepository.findCardCountsByRechargeTime(null, null);
			statistics.setTodayCardRechargedCounts(todayCardRechargedCounts);
			statistics.setYesterdayCardRechargedCounts(yesterdayCardRechargedCounts);
			statistics.setWeekCardRechargedCounts(weekCardRechargedCounts);
			statistics.setMonthCardRechargedCounts(monthCardRechargedCounts);
			statistics.setTotalCardRechargedCounts(totalCardRechargedCounts);
			// ------------------------------------------------------------------------------------------
			Double todayRechargedPoints = pointRechargeCardRepository.findCardPointsByRechargeTime(now, now);
			Double yesterdayRechargedPoints = pointRechargeCardRepository.findCardPointsByRechargeTime(yesterday,
					yesterday);
			Double weekRechargedPoints = pointRechargeCardRepository.findCardPointsByRechargeTime(weekStartDay,
					weekEndDay);
			Double monthRechargedPoints = pointRechargeCardRepository.findCardPointsByRechargeTime(monthStartDay,
					monthEndDay);
			Double totalRechargedPoints = pointRechargeCardRepository.findCardPointsByRechargeTime(null, null);
			statistics.setTodayRechargedPoints(todayRechargedPoints);
			statistics.setYesterdayRechargedPoints(monthRechargedPoints);
			statistics.setWeekRechargedPoints(weekRechargedPoints);
			statistics.setMonthRechargedPoints(yesterdayRechargedPoints);
			statistics.setTotalRechargedPoints(totalRechargedPoints);
			// -----------------------------------------------------------------------------------------------------------
		}
		Long todayOrderCounts = orderRepository.findPayedOrderCountsByPayTime(now, now, apiClientId);
		Long yesterdayOrderCounts = orderRepository.findPayedOrderCountsByPayTime(yesterday, yesterday, apiClientId);
		Long weekOrderCounts = orderRepository.findPayedOrderCountsByPayTime(weekStartDay, weekEndDay, apiClientId);
		Long monthOrderCounts = orderRepository.findPayedOrderCountsByPayTime(monthStartDay, monthEndDay, apiClientId);
		Long totalOrderCounts = orderRepository.findPayedOrderCountsByPayTime(null, null, apiClientId);
		statistics.setTodayOrderPayedCounts(todayOrderCounts);
		statistics.setYesterdayOrderPayedCounts(yesterdayOrderCounts);
		statistics.setWeekOrderPayedCounts(weekOrderCounts);
		statistics.setMonthOrderPayedCounts(monthOrderCounts);
		statistics.setTotalOrderPayedCounts(totalOrderCounts);
		// -----------------------------------------------------------------------------------------------
		Double todayOrderPoints = orderRepository.findPayedOrderPointsByPayTime(now, now, apiClientId);
		Double yesterdayOrderPoints = orderRepository.findPayedOrderPointsByPayTime(yesterday, yesterday, apiClientId);
		Double weekOrderPoints = orderRepository.findPayedOrderPointsByPayTime(weekStartDay, weekEndDay, apiClientId);
		Double monthOrderPoints = orderRepository.findPayedOrderPointsByPayTime(monthStartDay, monthEndDay,
				apiClientId);
		Double totalOrderPoints = orderRepository.findPayedOrderPointsByPayTime(null, null, apiClientId);
		statistics.setTodayOrderPayedPoints(todayOrderPoints);
		statistics.setYesterdayOrderPayedPoints(yesterdayOrderPoints);
		statistics.setWeekOrderPayedPoints(weekOrderPoints);
		statistics.setMonthOrderPayedPoints(monthOrderPoints);
		statistics.setTotalOrderPayedPoints(totalOrderPoints);
		// ---------------------------------------------------------------------------------------------------------
		Long todayTopicCounts = topicRepository.findTopicsCountsByCreateTime(now, now, apiClientId);
		Long yesterdayTopicCounts = topicRepository.findTopicsCountsByCreateTime(yesterday, yesterday, apiClientId);
		Long weekTopicCounts = topicRepository.findTopicsCountsByCreateTime(weekStartDay, weekEndDay, apiClientId);
		Long monthTopicCounts = topicRepository.findTopicsCountsByCreateTime(monthStartDay, monthEndDay, apiClientId);
		Long totalTopicCounts = topicRepository.findTopicsCountsByCreateTime(null, null, apiClientId);
		statistics.setTodayTopicCounts(todayTopicCounts);
		statistics.setYesterdayTopicCounts(yesterdayTopicCounts);
		statistics.setWeekTopicCounts(weekTopicCounts);
		statistics.setMonthTopicCounts(monthTopicCounts);
		statistics.setTotalTopicCounts(totalTopicCounts);
		// ---------------------------------------------------------------------------------------------------------
		Long todayMemberCounts = memberRepository.findMemberCountsByRegisterTime(now, now, apiClientId);
		Long yesterdayMemberCounts = memberRepository.findMemberCountsByRegisterTime(yesterday, yesterday, apiClientId);
		Long weekMemberCounts = memberRepository.findMemberCountsByRegisterTime(weekStartDay, weekEndDay, apiClientId);
		Long monthMemberCounts = memberRepository.findMemberCountsByRegisterTime(monthStartDay, monthEndDay,
				apiClientId);
		Long totalMemberCounts = memberRepository.findMemberCountsByRegisterTime(null, null, apiClientId);
		statistics.setTodayMemberCounts(todayMemberCounts);
		statistics.setYesterdayMemberCounts(yesterdayMemberCounts);
		statistics.setWeekMemberCounts(weekMemberCounts);
		statistics.setMonthMemberCounts(monthMemberCounts);
		statistics.setTotalMemberCounts(totalMemberCounts);
		
		statistics.setApiClientId(apiClientId);

		if (flag == true) {
			statisticsRepository.update(statistics);
		} else {
			statisticsRepository.save(statistics);
		}

		return statistics;
	}

}
